﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        Start start; 
        New_Account newac;
        Main main; //possible windows
        public MainWindow()
        {
            InitializeComponent();
            Begin();
            
        }
        private void Begin()
        {
            start = new Start(); //initialize first page
            start.Connected += new Start.ConnectedHandler(start_Connected);
            start.LoggedIn += new Start.LoggedInHandler(LoggedIn);
            start.NewAccountRequest += new Start.NewAccountRequestHandler(start_NewAccountRequest);

            frame.Navigate(new Page1()); //starts by displaying 'trying to connect to server'

            start.Connect(); //begins trying to connect to server           

        }
        private void start_Connected() //when connected to server
        {
            frame.Navigate(start); //goes to log in page            
            newac = new New_Account(start.client); //initialize new account page
            newac.LoggedIn+=new New_Account.LoggedInHandler(LoggedIn);
            //waits for log in
        }
        private void LoggedIn()
        {
            ToMain(); //navigates to main page
        }
        private void start_NewAccountRequest()
        {
            ToNewAccount(); //navigates to new account
        }
        private void ToMain()
        {
            frame.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate() //in main thread
            {
                frame.NavigationUIVisibility = NavigationUIVisibility.Hidden; //hide navigation bar (to avoid users going back)
                Height = 700; //increases height of window
                main = new Main(start.client); //initializes main page  
                frame.Navigate(main); // go to main page
                main.LogOut+=new Main.LogOutHandler(main_LogOut);//start waiting for log out
            });
            
        }
        private void main_LogOut()
        {
                frame.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate() //in main thread
                {
                    Height = 300; //back to original size
                    Begin(); //start from the beggining
                });

         }

        private void ToNewAccount()
        {
            frame.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate() //in main thread
            {
               frame.Navigate(newac);//navigates to new account page
            });
        }
        private void Navigate(Object page)
        {
            frame.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate() //in main thread
            {
                frame.Navigate((Page)page); //navigated to given page
            });
        }

    }
}
